{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Readme \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dependence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Version 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FC(nn.Module):\n",
    "    \"\"\"Fully Connected Network 全连接层\n",
    "    包含ReLU active function 和 Dropout 层\n",
    "\n",
    "    Args:\n",
    "        nn (_type_): pytorch base class\n",
    "    \"\"\"    \n",
    "    def __init__(self, in_size :int, out_size :int, dropout_r :float = 0., use_relu :bool =True):\n",
    "        \"\"\"init function\n",
    "\n",
    "        Args:\n",
    "            in_size (int): 输入特征的维度\n",
    "            out_size (int): 输出特征的维度\n",
    "            dropout_r (float, optional): Dropout比率，用于防止过拟合. Defaults to 0..\n",
    "            use_relu (bool, optional): 是否使用ReLU激活函数. Defaults to True.\n",
    "        \"\"\"        \n",
    "        super(FC, self).__init__()\n",
    "        self.dropout_r = dropout_r\n",
    "        self.use_relu = use_relu\n",
    "\n",
    "        # 将输入特征映射到输出特征\n",
    "        self.linear = nn.Linear(in_size, out_size)\n",
    "\n",
    "        if use_relu:\n",
    "            self.relu = nn.ReLU(inplace=True)\n",
    "\n",
    "        if dropout_r > 0:\n",
    "            self.dropout = nn.Dropout(dropout_r)\n",
    "\n",
    "    def forward(self, x :'torch.Tensor'):\n",
    "        \"\"\"前向传播\n",
    "\n",
    "        Args:\n",
    "            x (torch.Tensor): 输入的张量\n",
    "\n",
    "        Returns:\n",
    "            (torch.Tensor): 返回处理后的张量\n",
    "        \"\"\"        \n",
    "        x = self.linear(x)\n",
    "\n",
    "        # 如果use_relu为True，则通过ReLU激活函数处理x\n",
    "        if self.use_relu:\n",
    "            x = self.relu(x)\n",
    "\n",
    "        # 如果dropout_r大于0，则通过Dropout层处理x\n",
    "        if self.dropout_r > 0:\n",
    "            x = self.dropout(x)\n",
    "\n",
    "        return x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Version 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class FullyConnectedNetwork(nn.Module):\n",
    "    def __init__(self, input_size, hidden_size, output_size):\n",
    "        super(FullyConnectedNetwork, self).__init__()\n",
    "        self.fc1 = nn.Linear(input_size, hidden_size)\n",
    "        self.relu = nn.ReLU()\n",
    "        self.fc2 = nn.Linear(hidden_size, output_size)\n",
    "\n",
    "    def forward(self, x):\n",
    "        out = self.fc1(x)\n",
    "        out = self.relu(out)\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例化网络\n",
    "input_size = 784  # 例如，对于28x28的MNIST图像\n",
    "hidden_size = 500\n",
    "output_size = 10  # 例如，MNIST的10个类别\n",
    "net = FullyConnectedNetwork(input_size, hidden_size, output_size)\n",
    "\n",
    "# 创建一个随机输入张量来测试网络\n",
    "input_tensor = torch.randn(1, input_size)\n",
    "\n",
    "# 通过网络传递输入张量\n",
    "output = net(input_tensor)\n",
    "print(output)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
